package com.zy.kotlinbaselibrary.base.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.zy.kotlinbaselibrary.base.mvp.MvpRxActivity
import kotlinx.android.synthetic.main.toolbar.*

abstract class BaseActivity: MvpRxActivity() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId(savedInstanceState))

        configToolbar()

        initData()
        initEvent()
    }


    fun configToolbar(){
        toolbar?.setToolbarBackOnClick(View.OnClickListener { finish() })
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    /**
     * 替代onCreate的使用
     */
    protected abstract fun getLayoutId(savedInstanceState: Bundle?): Int

    /**
     * 初始化数据
     */
    protected abstract fun initData()

    /**
     * 初始化监听方法
     */
    protected abstract fun initEvent()

    //Toast显示
    protected fun showShortToast(string: String) {

        Toast.makeText(this, string, Toast.LENGTH_SHORT).show()
    }

    protected fun showLongToast(string: String) {
        Toast.makeText(this, string, Toast.LENGTH_LONG).show()
    }

    /**
     * 界面跳转
     *
     * @param clazz 目标Activity
     */
    protected fun readyGo(clazz: Class<*>) {
        readyGo(clazz, null)
    }

    /**
     * 跳转界面，  传参
     *
     * @param clazz  目标Activity
     * @param bundle 数据
     */
    protected fun readyGo(clazz: Class<*>, bundle: Bundle?) {
        val intent = Intent(this, clazz)
        if (null != bundle)
            intent.putExtras(bundle)
        startActivity(intent)
    }

    /**
     * 跳转界面并关闭当前界面
     *
     * @param clazz 目标Activity
     */
    protected fun readyGoThenKill(clazz: Class<*>) {
        readyGoThenKill(clazz, null)
    }

    /**
     * @param clazz  目标Activity
     * @param bundle 数据
     */
    protected fun readyGoThenKill(clazz: Class<*>, bundle: Bundle?) {
        readyGo(clazz, bundle)
        finish()
    }

    /**
     * startActivityForResult
     *
     * @param clazz       目标Activity
     * @param requestCode 发送判断值
     */
    protected fun readyGoForResult(clazz: Class<*>, requestCode: Int) {
        val intent = Intent(this, clazz)
        startActivityForResult(intent, requestCode)
    }

    /**
     * startActivityForResult with bundle
     *
     * @param clazz       目标Activity
     * @param requestCode 发送判断值
     * @param bundle      数据
     */
    protected fun readyGoForResult(clazz: Class<*>, requestCode: Int, bundle: Bundle?) {
        val intent = Intent(this, clazz)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent, requestCode)
    }


}